<?php 
$page_security = 'SA_SALESPAYMNT';
$path_to_root = "..";
include_once($path_to_root . "/includes/ui/allocation_cart.inc");
include_once($path_to_root . "/includes/session.inc");
include_once($path_to_root . "/includes/date_functions.inc");
include_once($path_to_root . "/includes/ui.inc");
include_once($path_to_root . "/includes/banking.inc");
include_once($path_to_root . "/includes/data_checks.inc");
include_once($path_to_root . "/sales/includes/sales_db.inc");
//include_once($path_to_root . "/sales/includes/ui/cust_alloc_ui.inc");
include_once($path_to_root . "/reporting/includes/reporting.inc");
include("/includes/db/pittycash_db.inc");

$js = "";
if ($use_popup_windows) {
	$js .= get_js_open_window(900, 500);
}
if ($use_date_picker) {
	$js .= get_js_date_picker();
}
add_js_file('payalloc.js');

page(_($help_context = "Pengisian Kas Bon"), false, false, "", $js);
simple_page_mode();
//var_dump($Mode);
//var_dump($selected_id);

if($Mode == 'Delete')
{
//	var_dump($_POST);
//	var_dump($selected_id);
	del_kas_bon_details(get_post('kasbon_id'),$selected_id);
		unset($_POST);
	//unset($_SESSION);
}

if(isset($_GET['NewTrans']))
	unset($_SESSION['kb_Items']);

if(isset($_SESSION['kb_Items']))
	$line_items = $_SESSION['kb_Items'];
else 
	$line_items = array();
	
//var_dump($line_items);	
$Refs = new references();



if (isset($_GET['AddedID'])) {
	$payment_no = $_GET['AddedID'];

	display_notification_centered(_("The customer payment has been successfully entered."));

	submenu_print(_("&Print This Receipt"), ST_CUSTPAYMENT, $payment_no."-".ST_CUSTPAYMENT, 'prtopt');

	display_note(get_gl_view_str(ST_CUSTPAYMENT, $payment_no, _("&View the GL Journal Entries for this Customer Payment")));

//	hyperlink_params($path_to_root . "/sales/allocations/customer_allocate.php", _("&Allocate this Customer Payment"), "trans_no=$payment_no&trans_type=12");

	hyperlink_no_params($path_to_root . "/sales/customer_payments.php", _("Enter Another &Customer Payment"));
	
	display_footer_exit();
}


//----------------------------------------------------------------------------------------------

function can_process()
{
	global $Refs;

		return true;
}

if (isset($_POST['AddPaymentItem'])) {
	
	
	save_kas_bon_details(get_post('pref'), get_post('kasbon_id'), $line_items);
	
	unset($_POST);
	unset($_SESSION);
	
		
//
//	//Chaitanya : 13-OCT-2011 - To support Edit feature
	meta_forward($_SERVER['PHP_SELF'], "NewTrans=0");
//	meta_forward($_SERVER['PHP_SELF'], $new ? "AddedID=$payment_no" : "UpdatedID=$payment_no");
}

//----------------------------------------------------------------------------------------------
//$_POST['ref'] = $Refs->get_next(ST_KASBON);
//$_POST['date'] = Today();

start_form();

//	hidden('trans_no', $_POST['trans_no']);
//	hidden('old_ref', $old_ref);

	start_outer_table(TABLESTYLE2, "width=75%", 5);
	table_section(1);

	//if ($new)
	//customer_list_row(_("Employee:"), 'customer_id', null, false, true);
	kasbon_list_row("Kas Bon Ref:", 'kasbon_id', null, false, true);
	$kasbon_id = $_POST['kasbon_id'];
	//display_notification($kasbon_id);
	$row = get_kas_bon($kasbon_id);
	$pRef = $Refs->get_next(ST_RETKASBON);
	//var_dump($row);
//	var_dump(get_post('kasbon_id'));	
//	var_dump($_POST['kasbon_id']);
	//$_POST['amount'] = $row['amount'];
	//label_cells(_("Total Amount:"), $kasbon_id,"class='label'");
    label_row("Date", $row['date']);
	label_row(_("Total Amount:"), price_format($row['amount']));
	label_row(_("Reference:"), $pRef);
	hidden('pref',$pRef);
	table_section(2);
	textarea_row(_("Keperluan:"), 'note', $row['note'], 50,5);
	//label_row($label, $value)
	//textarea_cells("Keperluan", 'note', null, 22, 2);	
	//set_global_customer($_POST['customer_id']);




//		table_section(3);

//		date_row(_("Date of Deposit:"), 'DateBanked', '', true, 0, 0, 0, null, true);


		end_outer_table(1);


//		start_table(TABLESTYLE, "width=60%");
//
//		
//
//		
//		end_table(1);

		br();
	if (isset($_POST['AddItem']))
	{
		$new = array('no_note' => get_post('no_note'),
		'acc_id' => get_post('acc_id'),
		'note' => get_post('note'),
		'amount' => input_num('amount'));
		
		//array_append($line_items,$new);
		//$line_items->
		array_push($line_items, $new);
		//var_dump($line_items);
		save_kas_bon_details(get_post('pref'), get_post('kasbon_id'), $line_items);
		unset($_POST);
	//unset($_SESSION);
		$Ajax->activate('kb_Items');		
		$_SESSION['kb_Items'] = $line_items;
		
		//$_POST['line_items'] = $line_items;
			
	}
	
		$line_items = array();
		$result = get_kas_bon_details($kasbon_id);
		while ($stock_item = db_fetch($result))
				array_push($line_items, $stock_item);
		$_SESSION['kb_Items'] = $line_items;		
			
		//display_heading($title);

    //div_start('items_table');
	start_table(TABLESTYLE, "colspan=7 width=75%");
	//start_outer_table(TABLESTYLE2, "width=90%", 5);
	$th = array(_("No. Nota"), _("Kode Beban"), _("Keterangan"),_("Jumlah"),"");

//	if ($order->trans_no == 0) {
//	unset( $th[3] );
//	}

//	if (count($order->line_items))
//	     $th[]= '';

	table_header($th);

	$total = 0;
	$k = 0;  //row colour counter

	$id = find_submit('Delete');
	$has_marked = false;
	
	//if(count($line_items) < 1)
		
	foreach ($line_items as $line_no=>$stock_item)
	{
		$total += $stock_item['amount'];
		label_cell($stock_item['no_note']);
		label_cell($stock_item['acc_id']);
		label_cell($stock_item['note']);
		label_cell(price_format($stock_item['amount']),'align=right');
		//label_cell($stock_item['']);
		delete_button_cell("Delete".$stock_item['id'], _("Delete"),
				_('Remove line from document'));
		end_row();
	}
	
	alt_table_row_color($k);
	start_row();
	//qty_cells(null, 'qty', $_POST['qty'], null, null, $dec);
	if(!isset($_POST['no_note']))
		$_POST['no_note'] = '';
		
	text_cells(null, 'no_note',$_POST['no_note'],20);
//	if ($order->trans_no!=0) {
//		qty_cell($line_no==-1 ? 0 :$order->line_items[$line_no]->qty_done, false, $dec);
//	}

	//gl_all_accounts_list_row(null, 'acc_id');
	gl_all_accounts_list_cells(null, 'acc_id');
	//display_gl_controls($supp_trans, $k);
	
	//label_cell($units, '', 'units');
	text_cells(null, 'note','',20,40);
	
	//amount_cells(null, 'price','0');
	
	//textarea_cells(null, 'note', '', 20, 1);
	
	amount_cells(null, 'amount');

	//small_amount_cells(null, 'Disc', percent_format($_POST['Disc']), null, null, user_percent_dec());

	$line_total = input_num('qty') * input_num('price') * (1 - input_num('Disc') / 100);

	//amount_cell($line_total, false, '','line_total');

	
		submit_cells('AddItem', _("Add Item"), "colspan=2 align='center'",
		    _('Add new item to document'), false);
	
	end_row();
//		}
//
//		$total += $line_total;
//	}

//	if ($id==-1 && $editable_items)
//		sales_order_item_controls($order, $k);

	$colspan = 3;
//	if ($order->trans_no!=0)
//		++$colspan;
	
//	$display_sub_total = price_format($total + input_num('freight_cost'));
//	label_row(_("Sub-total"), $display_sub_total, "colspan=$colspan align=right","align=right", 2);
//	$taxes = $order->get_taxes(input_num('freight_cost'));
//	$tax_total = display_edit_tax_items($taxes, $colspan, $order->tax_included, 2);

	$display_total = price_format($total);

	start_row();	
	label_cells(_("Amount Total"), $display_total, "colspan=$colspan align=right","align=right");
	submit_cells('update', _("Update"), "colspan=2 align='center'", _("Refresh"), true);
	end_row();
	
	
	//end_table();
	end_table();
//	if ($has_marked) {
//		display_note(_("Marked items have insufficient quantities in stock as on day of delivery."), 0, 1, "class='stockmankofg'");
//		if ($order->trans_type!=30 && !$SysPrefs->allow_negative_stock())
//			display_error(_("The delivery cannot be processed because there is an insufficient quantity for item:")
//				. '<br>'. $qoh_msg);
//	}
    //div_end();
		
	
	
	

	br();
	
	    //submit_center('AddPaymentItem', _("Save"), true, '', 'default');


	br();

end_form();
end_page();

?>